﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class AnswerDAL
    {
        #region Add
        public void Add(AnswerInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@QuestionID",SqlDbType.Int,4,info.QuestionID),
                db.MakeInParam("@ReviewID",SqlDbType.Int,4,info.ReviewID),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),
                db.MakeInParam("@Answer",SqlDbType.VarChar,300,info.Answer),
                db.MakeInParam("@CreateDate",SqlDbType.DateTime,8,info.CreateDate)
            };
            try
            {
                db.ExecNonQuery("T2R_Answer_Add", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        public DataTable GetByReviewID(int reviewid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@ReviewID", SqlDbType.Int, 4, reviewid)
                };
                return db.GetDataSet("T2R_Answer_GetByReviewID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetByQuestionID(int questionid, string userguid, DateTime begintime, DateTime endtime)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@QuestionID", SqlDbType.Int, 4, questionid),
                    db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(userguid)),
                    db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                    db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime)
                };
                return db.GetDataSet("T2R_Answer_GetByQuestionID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }

        public DataTable GetListByQuestionID(int questionid, string userguid, DateTime begintime, DateTime endtime)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@QuestionID", SqlDbType.Int, 4, questionid),
                    db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(userguid)),
                    db.MakeInParam("@BeginTime", SqlDbType.DateTime, 8, begintime),
                    db.MakeInParam("@EndTime", SqlDbType.DateTime, 8, endtime)
                };
                return db.GetDataSet("T2R_Answer_GetListByQuestionID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
    }
}
